package com.etoc.test.flow.kd;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.etoc.test.util.DateUtil;
import com.etoc.test.util.EncryptUtil;
import com.etoc.test.util.HttpUtil;

/**
 * 
 * �û�����Ϣ��ѯʵ����
 * <������ϸ����>
 * 
 * @author  �³�
 * @version  [�汾��, 2015-8-10]
 * @see  [�����/����]
 * @since  [��Ʒ/ģ��汾]
 */
public class QueryUserInfo implements Serializable {
	/**
	 * ע������
	 */
	private static final long serialVersionUID = -4200761846959292926L;
	private String action;
	private String appKey;
	private String phoneNo;
	private String model;
	private String timeStamp;
	private String sign;
	private String appSecret;
	Map<String, String> map = new TreeMap<String, String>();
	
	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getAppKey() {
		return appKey;
	}

	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}

	public String getPhoneNo() {
		return phoneNo;
	}

	public void setPhoneNo(String phoneNo) {
		this.phoneNo = phoneNo;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public String getTimeStamp() {
		return timeStamp;
	}

	public void setTimeStamp(String timeStamp) {
		this.timeStamp = timeStamp;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getAppSecret() {
		return appSecret;
	}

	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}
	
	public Map<String, String> getMap() {
		return map;
	}
	
	public String signString() {	
		if (this.action != null && !this.action.equals(""))
		{
			map.put("action", this.action);
		}
		if (this.appKey != null && !this.appKey.equals(""))
		{
			map.put("appKey", this.appKey);
		}
		if (this.model != null && !this.model.equals(""))
		{
			map.put("model", this.model);
		}
		if (this.phoneNo != null && !this.phoneNo.equals(""))
		{
			map.put("phoneNo", this.phoneNo);
		}
		if (this.timeStamp != null && !this.timeStamp.equals(""))
		{
			map.put("timeStamp", this.timeStamp);
		}
		
		Iterator<String> keySet = map.keySet().iterator();
		StringBuffer signString = new StringBuffer(this.appSecret);
		while (keySet.hasNext())
		{
			String key = keySet.next();
			String val = map.get(key);
			signString.append(key).append(val);
		}
		signString.append(this.appSecret);
		return signString.toString(); 
	}
	
	@Override
	public String toString() {
		Iterator<String> keySet = map.keySet().iterator();
		String ret = "";
		while (keySet.hasNext())
		{
			String key = keySet.next();
			String val = map.get(key);
			ret += key + "=" + val + "&";
		}
		ret += "sign" + "=" + sign;
		return ret;
	}
	
	public static void main(String[] args) throws Exception
	{
		QueryUserInfo order = new QueryUserInfo();
		order.setAction("queryUserBaseInfo");
		order.setAppKey("4aea47a6465e3de901465e68c8100000");
		order.setAppSecret("unicom");
		order.setPhoneNo("18671326122");
		order.setModel("0");
		order.setTimeStamp(DateUtil.dateToStr(new Date(), "yyyyMMddHHmmss"));
		String sign = order.signString();
		order.setSign(EncryptUtil.encryptSHA(sign));
		
		String url = "http://113.57.243.18:8070/flowAgent";
		// http://100.0.4.8:8080/flowAgent?
		System.out.println(url + "?" + order.toString());
		
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		Map<String, String> map = order.getMap();
		Iterator<String> keySet = map.keySet().iterator();
		while (keySet.hasNext())
		{
			String key = keySet.next();
			String val = map.get(key);
			
			nvps.add(new BasicNameValuePair(key, val));
		}
		nvps.add(new BasicNameValuePair("sign", order.getSign()));
		
		long startTime = System.currentTimeMillis();
		String respCode = HttpUtil.httpRequest(url, nvps);
		long endTime = System.currentTimeMillis();
		System.out.println("queryUserBaseInfo=" + respCode);
		System.out.println("spend " + (endTime - startTime) + " milli second.");
		
		/*String data = "2A8FADCA9D486D92A3CF11BCA7AA9E84C17DC9D6AB9676BC5086F46E607545ABE8C7C2C9C87514185A274372047600AF013B5741DCC6E58AA59004FD3855E373C2B2B4D887EC0F511D683A58B8FB90A77414400DDBEE327AF67A1649A9ADB9047FBCA7089CEE0B260587D15E00882660F70AE643395C8A93CCE758E2873BEA25D60B32A51A0388A3146B2EB63EEF24C20A2558CFF9E71D7D13E399EA62A1C07CA7D47AD475CF92796CA07E2DB8D6F791B669B166218706008A05DB7AD4E14F134690102E9A8457495C9228EB29C60760E57AEAAD6F34ED68C074D42E32C3F7BA9C75D30AB95B786090D84F2B3E89D2FC09E0F77683EBF495DD803F07F9020D355D045FE38A90AB72861D9811F630225F";
		// cpPassword����С��8λ����Ĭ��ֵ12345678
		String result = EncryptUtil.decryptDES(data, "12345678", null);
		System.out.println(result);*/
	}
	
}
